package com.foreknow.hospital.dao.impl;

import com.foreknow.hospital.dao.OrdersDao;
import com.foreknow.hospital.po.Hospital;
import com.foreknow.hospital.po.Orders;
import com.foreknow.hospital.po.Setmeal;
import com.foreknow.hospital.po.User;
import com.foreknow.hospital.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
        //连接数据库
        Connection con= DBUtil.getConnection();
        PreparedStatement pst;
        int state;
        @Override
        public int getOrdersByUserId(Orders order) {
            String sql="select state from orders where userId=?";
            try{
                //获取与处理对象
                pst=con.prepareStatement(sql);
                pst.setString(1, order.getUserId());
                ResultSet rs=pst.executeQuery();
                if(rs.next()){
                    //获取值
                    state=rs.getInt(1);
                }
            }
            catch (
                    SQLException e) {
                throw new RuntimeException(e);
            }
            finally {
                //关闭资源
                DBUtil.close(pst);
            }
            return state;
        }

        @Override
        public int saveOrders(Orders order) {
            int result=0;
            String sql="insert into orders(orderDate, userId, hpId, smId,state) values(?,?,?,?,1)";
            try{
                con = DBUtil.getConnection();
                pst = con.prepareStatement(sql);
                pst.setString(1, order.getOrderDate());
                pst.setString(2, order.getUserId());
               // pst.setString(3,order.getUserId());
                pst.setInt(3,order.getHpId());
                pst.setInt(4,order.getSmId());
                result = pst.executeUpdate();
            }
            catch (SQLException e) {
                throw new RuntimeException(e);
            }
            finally {
                DBUtil.close(pst);
            }
            return result;
        }

        @Override
        public int removeOrders(Orders order) {
            int result=0;
            String sql="delete from orders where orderId=?";
            try{
                con = DBUtil.getConnection();
                pst = con.prepareStatement(sql);
                pst.setInt(1, order.getOrderId());
                result = pst.executeUpdate();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            finally {
                DBUtil.close(pst);
            }
            return result;
        }

        @Override
        public Orders getOrdersById(Orders order) {
            Orders orders1=new Orders();
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT o.orderDate oorderDate, ");
            sql.append("u.realName uname, ");
            sql.append("u.identityCard uidentityCard, ");
            sql.append("u.birthday  ubirthday, ");
            sql.append("u.userId uuserId, ");
            sql.append("h.name hname, ");
            sql.append("h.businessHours hbusinessHours, ");
            sql.append("h.deadline hdeadline, ");
            sql.append("h.telephone htelephone, ");
            sql.append("h.address haddress, ");
            sql.append("s.name sname,");
            sql.append("s.price price ");
            sql.append("FROM orders o left JOIN users u ON u.userId=o.userId ");
            sql.append("LEFT JOIN hospital h ON o.hpId = h.hpId ");
            sql.append("LEFT JOIN setmeal s ON o.smId=s.smId ");
            sql.append("where o.orderId=?");
            try{
                //获取与处理对象
                pst=con.prepareStatement(sql.toString());
                //pst=con.prepareStatement(sql.toString());
                int orderId=order.getOrderId();
                pst.setInt(1, orderId);
                ResultSet rs=pst.executeQuery();
                if(rs.next()){
                    orders1.setOrderDate(rs.getString("oorderDate"));
                    User user=new User();
                    user.setRealName(rs.getString("uname"));
                    user.setIdentityCard(rs.getString("uidentityCard"));
                    user.setBirthday(rs.getString("ubirthday"));
                    user.setUserId(rs.getString("uuserId"));
                    Hospital hospital=new Hospital();
                    hospital.setName(rs.getString("hname"));
                    hospital.setBusinessHours(rs.getString("hbusinessHours"));
                    hospital.setDeadline(rs.getString("hdeadline"));
                    hospital.setTelephone(rs.getString("htelephone"));
                    hospital.setAddress(rs.getString("haddress"));
                    Setmeal setmeal=new Setmeal();
                    setmeal.setName(rs.getString("sname"));
                    setmeal.setPrice(rs.getInt("price"));
                    orders1.setUser(user);
                    orders1.setHospital(hospital);
                    orders1.setSetmeal(setmeal);
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }finally {
                DBUtil.close(pst);
            }
            return orders1;
        }
        @Override
        public List<Orders> listOrdersByUserId(Orders order) {
            List<Orders> list = new ArrayList<>();
            Connection con = null;
            PreparedStatement pst = null;
            ResultSet rs = null;
          String sql="select o.orderId,o.orderDate,s.name\n" +
                  "FROM orders o,setmeal s \n" +
                  "WHERE o.smId=s.smId\n" +
                  "and o.userId=? and o.state=?";
            try {
                con = DBUtil.getConnection();
                pst = con.prepareStatement(sql.toString());
                pst.setString(1, order.getUserId());
                pst.setInt(2, order.getState());
                rs = pst.executeQuery();
                while (rs.next()) {
                    Orders orders1 = new Orders();
                    orders1.setOrderDate(rs.getString("o.orderDate"));
                    orders1.setOrderId(rs.getInt("o.orderId"));
                    Setmeal setmeal = new Setmeal();
                    setmeal.setName(rs.getString("s.name"));
                    orders1.setSetmeal(setmeal);
                    list.add(orders1);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {

                DBUtil.close(pst);

            }
            return list;
        }

    }


